
class View
{
    constructor(man, name, callback, parent)
    {
        this.mViewMan = man

        this.mName    = name

        this.mRoot    = null

        let onLoaded = (sysnc) =>{

            if (this.mRoot)
            {
                if (parent)
                {
                    parent.addChild(this.mRoot)
                }
                else
                {
                    this.mViewMan.rootBy().addChild(this.mRoot)
                }
                
                if (sysnc)
                {
                    let com = this.mRoot.getComponent('ViewAnimation')
                    if (com)
                    {
                        com.start()
                    }
                }
                Log.d("open view ok: " + this.mName)
            }
            else
            {
                Log.w("open view fail: " + this.mName)
            }

            callback && callback(this, this.mRoot, sysnc)
        }

        {
            ogre.loadPrefab('ui/' + name, 3, 
                (res) => {

                    if (res) 
                    {
                        if (!this.mViewMan)
                        {
                            Log.w("open closed view : " + this.mName)

                            return
                        }
                
                        this.mRoot = cc.instantiate(res)
                    }
                    onLoaded()
                }
            )
        }
    }

    close()
    {
        if (this.mRoot)
        {
            this.quit && this.quit()

            if (this.mRoot.isValid)
            {
                {
                    this.mRoot.removeFromParent(true)

                    ogre.destroyNode(this.mRoot)

                    ogre.releasePrefab('ui/' + this.mName)
                }
            }
            this.mRoot    = null
        }

        this.mViewMan = null
    }

    /**
     * 当界面打开时，逻辑调用，子类需要重载
     *
     * @param {Object} d - 界面打开时，预传入给该界面的逻辑数据，可以为任意类型或者NULL
     */
    open(d)
    {

    }

    /**
     * 当界面关闭时，逻辑调用，子类需要重载
     */
    quit()
    {

    }

    /**
     * 启动界面帧循环，前提是当前界面的prefab根节点(this.mRoot)需要加入界面更新器组件ViewUpdater
     *
     * @param {Function} looper - 帧调用函数
     */
    startUpdate(looper)
    {
        if (this.mRoot)
        {
            let com = this.mRoot.getComponent('ViewUpdater')
            if (com)
            {
                com.looper = looper
            }
            else
            {
                Log.w('view not find view updater, ' + this.mName)
            }
        }
    }

    requestClose()
    {
        if (this.mRoot)
        {
            let com = this.mRoot.getComponent('ViewAnimation')
            if (com)
            {
                com.close(
                    ()=>{
                        this.close()
                    }
                )
                return true
            }
        }

        return false
    }

    /**
     * 查找当前界面下的节点
     * @param {String} name 需要查找的节点名称，如果是多级节点名称为parent下开始子节点名称连接"name.name.name'
     * @param {CC.Node} parent - 开始查找节点， 为NULL 是默认是界面的根节点
     * @return {cc.Node}
     */
    find(name, parent)
    {
        return ogre.seekNode(name, parent || this.mRoot)
    }

    widget(name, parent)
    {
        if (parent)
        {
            return parent.getChildByName(name)
        }
        return this.mRoot.getChildByName(name)
    }

    cachedView()
    {
        return true
    }
    
    /**
     * 返回界面名称
     * @return {String}
     */
    name()
    {
        return this.mName
    }

    root()
    {
        return this.mRoot
    }
    
    playSound(id)
    {
        let as = require('AudioSystem').share()
        as.play(id, false, false,
            (aid, finish)=>{

                if (!finish)
                {
                    return this.mRoot != null
                }
            }
        )
    }

    bindTouchEvent(parent, name, upHandler, sound)
    {
        if (!sound && sound !== '')
        {
            sound = 'click'
        }

        if (name)
        {
            return ogre.bindTouchEvent(parent, name, upHandler, sound)
        }
        return ogre.bindNodeTouchEvent(parent, upHandler, sound)
    }
    
    bindTouchEventAll(parent, name, upHandler, downHandler, moveHandler, cancelHandler)
    {
        if (name)
        {
            return ogre.bindTouchEventAll(parent, name, upHandler, downHandler, moveHandler, cancelHandler)
        }
        return ogre.bindNodeTouchEventAll(parent, upHandler, downHandler, moveHandler, cancelHandler)
    }

	spriteByHold(i)
	{
		if (this.mRoot)
        {
            let com = this.mRoot.getComponent('SpriteHolder')
            if (com)
            {
                return com.get(i)
            }
            else
            {
                Log.w('view not find "SpriteHolder", ' + this.mName)
            }
        }
		return null
    }
    
    replaceSpriteByHold(root, name, i)
    {
        let sprite = this.spriteByHold(i)
        if (!sprite)
        {
            return
        }

        let node = name ? this.find(name, root) : root
        if (node)
        {
            node = node.getComponent(cc.Sprite)

            if (node)
            {
                node.spriteFrame = sprite
            }
        }
    }
}

export default View

